package com.junjie.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.junjie.dao.IReceiveAddressDao;
import com.junjie.entity.ReceiveAddress;
import com.junjie.entity.User;
import com.junjie.util.ConfigUtil;
import com.junjie.util.DBUtil;

public class ReceiveAddressDaoImpl implements IReceiveAddressDao{
	/**
	 * 查询方法
	 */
	public List<ReceiveAddress> select(String sql ,Object[]parameter) throws Exception {
		List<ReceiveAddress> addressList = new ArrayList<ReceiveAddress>();
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		conn = DBUtil.getConnection();
		pstmt = conn.prepareStatement(sql);
		if(parameter !=null && parameter.length>0){
			for (int i = 1; i <= parameter.length; i++) {
				pstmt.setObject(i, parameter[i-1]);
			}
		}
		rs = pstmt.executeQuery();
		while(rs.next()){
			ReceiveAddress address = new ReceiveAddress();
			address.setId(rs.getInt("id"));
			address.setUserId(rs.getInt("user_id"));
			address.setFullAddress(rs.getString("full_address"));
			address.setReceiveName(rs.getString("receive_name"));
			address.setPostalCode(rs.getString("postal_code"));
			address.setPhone(rs.getString("phone"));
			address.setMobile(rs.getString("mobile"));
			addressList.add(address);
		}
		return addressList;
	}
	/**
	 * Update方法
	 * @throws SQLException 
	 */
	public void update(String sql,Object[] parameter) throws SQLException{
		Connection conn = null;
		PreparedStatement pstmt = null;
		conn = DBUtil.getConnection();
		pstmt = conn.prepareStatement(sql);
		if(parameter != null && parameter.length>0){
			for (int i = 1; i <= parameter.length; i++) {
				pstmt.setObject(i, parameter[i-1]);
			}
		int i =	pstmt.executeUpdate();
		if(i>0){
			System.out.println("更新成功");
		}
		}
	}
	/**
	 * 添加收件地址
	 * @throws SQLException 
	 */
	public void addReceviceAddress(ReceiveAddress address) throws SQLException{
		update(ConfigUtil.getValue("ADD_ADDRESS"),new Object[]{address.getUserId(),address.getFullAddress(),address.getReceiveName(),address.getPostalCode(),address.getMobile(),address.getPhone()});
	}
	/**
	 *根据ID 查询数据
	 * @throws Exception 
	 */
	public ReceiveAddress findById(int id) throws Exception{
		List<ReceiveAddress> addressList = select(ConfigUtil.getValue("FIND_ADDRESS_BY_ID"), new Object[]{id});
		if(addressList.size()>0){
			return addressList.get(0);
		}else{
			return null;
		}
	}
	/**
	 * 根据user.id查询地址信息
	 */
	public List<ReceiveAddress> findByUserId(User user)throws Exception{
		return select(ConfigUtil.getValue("FIND_BY_USERID"),new Object[]{user.getId()});
	}
	/**
	 * 根据地址查询信息,没有记录返回true,反之返回false;
	 * @param fullAddress
	 */
	public boolean findByAddress(String fullAddress)throws Exception {
		if(select(ConfigUtil.getValue("FIND_BY_ADDRESS"),new Object[]{fullAddress}).size()>0){
			return false;
		}else{
			return true;
		}
	}
}
